import { moveFrequency, roleSpeed as speed } from "./config.js";
import { fire } from "./action/shooting/fire.js";
import { boxH, boxW, currentRole, role } from "./index.js";
import { emit, on } from "./utils/scoket.js";

const setTransfrom = (po: { x: number; y: number }) => {
  return `translate(${po.x}px, ${po.y}px)`;
};

export const getPosition = (ele: HTMLElement) => {
  const po = ele.style.transform;
  const xy = po.slice(po.indexOf("(") + 1, po.indexOf(")")).split(",");
  return {
    x: +xy[0].replace("px", ""),
    y: +xy[1].replace("px", ""),
  };
};

export const getRolePosition = () => {
  return getPosition(role);
};

//指令动作
const actions = () => {
  const keys = Object.keys(scheduler);

  keys.forEach((k) => {
    //判断移动的位置是否是边界
    switch (k) {
      case "a":
        move("a");
        break;
      case "d":
        move("d");
        break;
      case "w":
        move("w");
        break;
      case "s":
        move("s");
        break;
      case "j":
        // fire(k);
        break;
    }
  });
  requestAnimationFrame(actions);
};
requestAnimationFrame(actions);

//移动指令
let timer: any = {};
const move = (key: string) => {
  //速度频率
  if (typeof timer[key] === "number") return;
  timer[key] = setTimeout(() => (timer[key] = undefined), 1000 / moveFrequency);
  const rolePosition = getRolePosition();
  switch (key) {
    case "a":
      if (rolePosition.x <= 0) return;
      rolePosition.x -= speed;
      role.style.transform = setTransfrom(rolePosition);
      break;
    case "d":
      if (rolePosition.x >= boxW) {
        rolePosition.x = boxW;
      } else {
        rolePosition.x += speed;
      }
      role.style.transform = setTransfrom(rolePosition);
      break;
    case "w":
      if (rolePosition.y <= 0) return;
      rolePosition.y -= speed;
      role.style.transform = setTransfrom(rolePosition);
      break;
    case "s":
      if (rolePosition.y + 10 >= boxH) {
        rolePosition.y = boxH;
      } else {
        rolePosition.y += speed;
      }
      role.style.transform = setTransfrom(rolePosition);
      break;
  }
};

//指令调度
const scheduler: { [key: string]: any } = {};
const schedulerp2: { [key: string]: any } = {};
//设置开始指令
export const setKey = (key: string) => {
  scheduler[key] || (scheduler[key] = true);
};

export const setKey2 = (key: string) => {
  schedulerp2[key] || (schedulerp2[key] = true);
};

//设置结束指令
export const removeKey = (key: string) => {
  scheduler[key] && delete scheduler[key];
};
export const removeKey2 = (key: string) => {
  scheduler[key] && delete scheduler[key];
};
//按键触发移动
let keyword = ["a", "s", "d", "w", "j"];
let lastKey = "";
export function transfrom(e: KeyboardEvent) {
  if (!keyword.includes(e.key)) return;
  if (lastKey === e.key) return;
  lastKey = e.key;

  // if (localStorage.role === "p1") {
  //   emit.p1("/key/down", { key: e.key });
  // } else if (localStorage.role === "p2") {
  //   emit.p2("/key/down", { key: e.key });
  // }
}
on.p1("/key/down", ({ value }) => {
  console.log(1);

  setKey(value.key);
});
on.p1("/key/up", ({ value }) => {
  removeKey(value.key);
});
// on.p2("/key/down", ({ value }) => {
//   setKey2(value.key);
// });
// on.p2("/key/up", ({ value }) => {
//   removeKey2(value.key);
// });

//停止移动
export function stopTransform(e: KeyboardEvent) {
  if (!keyword.includes(e.key)) return;
  lastKey = "";
  if (localStorage.role === "p1") {
    emit.p1("/key/up", { key: e.key });
  } else if (localStorage.role === "p2") {
    emit.p2("/key/up", { key: e.key });
  }
}
